package com.sikaryofficial.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sikaryofficial.backend.config.TenantFilterProperties;
import com.sikaryofficial.backend.domain.dto.req.BrandTasteReq;
import com.sikaryofficial.backend.domain.dto.req.ProductListReq;
import com.sikaryofficial.backend.domain.dto.req.QrCodeDownloadListReq;
import com.sikaryofficial.backend.domain.dto.resp.QrCodeDownloadDTO;
import com.sikaryofficial.backend.domain.entity.BrandTaste;
import com.sikaryofficial.backend.mapper.BrandTasteMapper;
import com.sikaryofficial.backend.service.IBrandTasteService;
import com.sikaryofficial.system.api.model.product.BrandTasteDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

/**
 * 品牌口味Service业务层处理
 *
 * @author qinjinyuan
 * @date 2023-10-27
 */
@Service
@SuppressWarnings(value = "unchecked")
public class BrandTasteServiceImpl extends ServiceImpl<BrandTasteMapper, BrandTaste> implements IBrandTasteService {
    @Autowired
    private BrandTasteMapper brandTasteMapper;
    @Autowired
    private TenantFilterProperties tenantFilterProperties;

    /**
     * 获取品牌口味列表
     *
     * @param req
     * @return
     */
    @Override
    public IPage<BrandTasteDTO> getBrandTasteList(BrandTasteReq req) {
        Page<BrandTasteDTO> customerPage = new Page<>(req.getPageNum(), req.getPageSize());
        return brandTasteMapper.getBrandTasteList(customerPage, req);
    }

    /**
     * 后台管理-产品分页列表
     *
     * @param customerPage
     * @param req
     * @return
     */
    @Override
    public IPage<BrandTasteDTO> productPageList(Page<BrandTasteDTO> customerPage, ProductListReq req, Date startTime, Date endTime) {
        return brandTasteMapper.productPageList(customerPage, req, startTime, endTime);
    }

    /**
     * 更新产品导出最大商品二维码个数
     *
     * @param md5    MD5字符串
     * @param maxNum 二维码生成个数
     * @param userId 用户id
     * @return
     */
    @Override
    public Long updateQrMaxNum(String md5, Long userId, long maxNum) {
        BrandTaste brandTaste = this.getOne(new LambdaQueryWrapper<BrandTaste>()
                .select(BrandTaste::getQrExportNum, BrandTaste::getBrandTasteId)
                .eq(BrandTaste::getMd5, md5)
                .eq(BrandTaste::getDeletedVersion, 0L)
                .last(" limit 1 ")
        );
        // 设置当前最大商品二维码个数
        Long qrExportNum = maxNum + brandTaste.getQrExportNum();
        brandTaste.setQrExportNum(qrExportNum);
        brandTaste.setUpdatedBy(userId);
        brandTaste.setUpdatedTime(new Date());
        this.updateById(brandTaste);
        return qrExportNum;
    }

    /**
     * 获取当前产品最大的短ID
     *
     * @return
     */
    @Override
    public Integer getMaxShortId() {
        BrandTaste brandTaste = this.getOne(new LambdaQueryWrapper<BrandTaste>()
                .eq(BrandTaste::getDeletedVersion, 0L)
                .last(" limit 1 ")
                .isNotNull(BrandTaste::getProductShortId)
                .orderByDesc(BrandTaste::getProductShortId)
        );
        if (Objects.isNull(brandTaste)) {
            return 1;
        }
        return brandTaste.getProductShortId() + 1;
    }

    /**
     * 后台管理-二维码下载列表
     *
     * @param customerPage
     * @param req
     * @param startDate
     * @param endDate
     * @return
     */
    @Override
    public IPage<QrCodeDownloadDTO> qrCodeDownloadList(Page<QrCodeDownloadDTO> customerPage, QrCodeDownloadListReq req, Date startDate, Date endDate) {
        return brandTasteMapper.qrCodeDownloadList(customerPage, req, startDate, endDate);
    }
}
